package com.hmx.jpa;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: SpringDataJPA
 * @description:
 * @author: hmx
 * @create: 2021-07-10 22:09
 **/

@NamedQuery(name = "testNamedQuery", query = "FROM Customer c WHERE c.id = ?0")
@Cacheable(true)
@Entity
@Table(name="JPA_CUSTOMERS")
public class Customer {

    public Customer() {
    }

    public Customer(String lastName, int age) {
        this.lastName = lastName;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }

    private Integer id;
    private String lastName;

    /**
     * 映射单向1-n 的关联关系
     * 使用 @OneToMany 来映射 1-n 的关联关系
     * 使用 @JoinColumn 来映射列的名称
     * 可以使用 @OneToMany 的 fetch 属性来修改默认的加载策略
     * 可以通过@OneToMany 的 cascade属性来修改默认的删除策略
     * @return
     */
    @OneToMany(
            //fetch=FetchType.EAGER,cascade={CascadeType.REMOVE})
    )
    @JoinColumn(name="CUSTOMER_ID")
    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    private String email;
    private int age;
    private Set<Order> orders = new HashSet<>();

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name="LAST_NAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
